<template>
  <div class="app-container">
    <div class="hr-line">
      <div class="other-title">配送员收益体现周期</div>
      <div class="other-content">
        <span class="des">每笔费用可在7天后体现到账户</span>
        <div class="des-input">
          <span>周期</span>
          <el-input v-model="form.yyCommonConfig.distributionClerkIncomeWithdrawalCycle" placeholder="7"></el-input>
          <span>天</span>
          <el-button type="text" @click="updateOther">确定</el-button>
        </div>
      </div>
    </div>
    <div class="hr-line">
      <div class="other-title">站长收益体现周期</div>
      <div class="other-content">
        <span class="des">每笔费用可在30天后体现到账户</span>
        <div class="des-input">
          <span>周期</span>
          <el-input v-model="form.yyCommonConfig.webmasterIncomeWithdrawalCycle" placeholder="30"></el-input>
          <span>天</span>
          <el-button type="text" @click="updateOther">确定</el-button>
        </div>
      </div>
    </div>
    <div class="hr-line">
      <div class="other-title">分拣预警</div>
      <div class="other-content">
        <span class="des">当前时间-预定送达时间小于预警时长则开始报警</span>
        <div class="des-input">
          <span>黄色预警时长</span>
          <el-input v-model="form.yyCommonConfig.sortingYellowWarning" placeholder="7"></el-input>
          <span>分钟</span>
          <span>红色预警时长</span>
          <el-input v-model="form.yyCommonConfig.sortingRedWarning" placeholder="7"></el-input>
          <span>分钟</span>
          <el-button type="text" @click="updateOther">确定</el-button>
        </div>
      </div>
    </div>
    <div class="hr-line">
      <div class="other-title">配送预警</div>
      <div class="other-content">
        <span class="des">当前时间-预定送达时间小于预警时长则开始报警</span>
        <div class="des-input">
          <span>黄色预警时长</span>
          <el-input v-model="form.yyCommonConfig.distributionYellowWarning" placeholder="7"></el-input>
          <span>分钟</span>
          <span>红色预警时长</span>
          <el-input v-model="form.yyCommonConfig.distributionRedWarning" placeholder="7"></el-input>
          <span>分钟</span>
          <el-button type="text" @click="updateOther">确定</el-button>
        </div>
      </div>
    </div>
    <div class="hr-line">
      <div class="other-title">证件过期预警</div>
      <div class="other-content">
        <span class="des">当前时间-证件过期时间小于预警时长则开始报警</span>
        <div class="des-input">
          <span>预警时长</span>
          <el-input v-model="form.yyCommonConfig.certificateExpirationWarning" placeholder="7"></el-input>
          <span>天</span>
          <el-button type="text" @click="updateOther">确定</el-button>
        </div>
      </div>
    </div>
    <div class="hr-line">
      <div class="other-title">配送间隔时长</div>
      <div class="other-content">
        <span class="des">用户下单可选时间范围按照当前配置的时间间隔计算</span>
        <div class="des-input">
          <span>时隔时长</span>
          <el-input v-model="form.yyCommonConfig.deliveryInterval" placeholder="60"></el-input>
          <span>分钟</span>
          <el-button type="text" @click="updateOther">确定</el-button>
        </div>
      </div>
    </div>
    <div class="hr-line">
      <div class="other-title">包装费</div>
      <div class="other-content">
        <span class="des">0元用户下单时不收取包装费，大于0元用户下单时需要收费对应金额的包装费</span>
        <div class="des-input">
          <span>包装费</span>
          <el-input v-model="form.yyCommonConfig.packingExpense" placeholder="60"></el-input>
          <span>元</span>
          <el-button type="text" @click="updateOther">确定</el-button>
        </div>
      </div>
    </div>
    <div class="hr-line">
      <div class="other-title">打卡范围</div>
      <div class="other-content">
        <span class="des">配送员需在所属驿站的N米范围内打卡</span>
        <div class="des-input">
          <span>距离</span>
          <el-input v-model="form.yyCommonConfig.punchRange" placeholder="60"></el-input>
          <span>米</span>
          <el-button type="text" @click="updateOther">确定</el-button>
        </div>
      </div>
    </div>
    <div class="hr-line">
      <div class="title">
        配送重量
        <el-icon
          ><Edit
            @click="addWeightIcon"
            style="width: 18px; height: 18px; font-size: 18px; color: #009688; vertical-align: middle; cursor: pointer"
        /></el-icon>
      </div>
      <div>
        <el-radio-group v-model="radio1">
          <div class="radio-btn" @click="selectWeight(item)" v-for="item in form.deliveryWeight" :key="item.id">
            <span @click.stop="btnRemove(item.id)">x</span>
            <div>
              <el-radio-button
                :label="item.minWeight + 'kg' + '-' + item.maxWeight + 'kg'"
                :value="item.id + '-' + item.maxWeight"
              ></el-radio-button>
            </div>
          </div>
        </el-radio-group>
      </div>
    </div>
    <div class="hr-line">
      <div class="title">
        配送时段
        <el-icon
          ><Edit
            @click="addTimeIcon"
            style="width: 18px; height: 18px; font-size: 18px; color: #009688; vertical-align: middle; cursor: pointer"
        /></el-icon>
      </div>
      <div>
        <el-radio-group v-model="radio1">
          <div class="radio-btn" @click="selectTime(item)" v-for="item in form.deliveryTime" :key="item.id">
            <span @click.stop="btnRemove2(item.id)">x</span>
            <div>
              <el-radio-button
                :label="item.startTime + '-' + item.endTime"
                :value="item.id + '-' + item.endTime"
              ></el-radio-button>
            </div>
          </div>
        </el-radio-group>
      </div>
    </div>
    <div class="hr-line">
      <div class="other-title">爬楼费</div>
      <div class="other-content">
        <span class="des">用户下单时根据所在户室楼层判断收取的爬楼费费用</span>
        <div class="des-input">
          <span>起步楼层</span>
          <el-input
            style="display: inline-block; width: 100px"
            v-model="form.yyCommonConfig.climbingFeeStartFloor"
            placeholder="7"
          ></el-input>
          <span>楼</span>
          <span>起步费用</span>
          <el-input
            style="display: inline-block; width: 100px"
            v-model="form.yyCommonConfig.climbingFeeStart"
            placeholder="7"
          ></el-input>
          <span>元</span>
          <span>间隔费用</span>
          <el-input
            style="display: inline-block; width: 100px"
            v-model="form.yyCommonConfig.climbingFeeStartSpace"
            placeholder="7"
          ></el-input>
          <span>元</span>
          <el-button type="text" @click="updateOther">确定</el-button>
        </div>
      </div>
    </div>
    <div class="hr-line">
      <div class="other-title">订单分拣</div>
      <div class="other-content">
        <span class="des"
          >订单分拣员同时选<span color="red">{{ form.yyCommonConfig.sortingmaxnum }}</span> 个订单进行分拣</span
        >
        <div class="des-input">
          <el-input
            style="display: inline-block; width: 100px"
            v-model="form.yyCommonConfig.sortingmaxnum"
            placeholder="7"
          ></el-input>
          <span>个</span>
          <el-button type="text" @click="updateOther">确定</el-button>
        </div>
      </div>
    </div>
    <div class="hr-line">
      <div class="other-title">取货订单数配置</div>
      <div class="other-content">
        <span class="des" style="display: inline-block; width: 50%"
          >配送员扫码订单成功后，若当前订单所厘驿站下的可配送订单大于等于3，则配送员可正常取货，若小于3则需判断该驿
          站是否存在达到红色预警的订单，存在则可正常取货，不存在则不能取货。</span
        >
        <div class="des-input">
          <el-input
            style="display: inline-block; width: 100px"
            v-model="form.yyCommonConfig.pickableOrderNum"
            placeholder="7"
          ></el-input>
          <span>个</span>
          <el-button type="text" @click="updateOther">确定</el-button>
        </div>
      </div>
    </div>
    <div class="hr-line">
      <div class="other-title">取货时长配置</div>
      <div class="other-content">
        <span class="des"
          >配送员取货中开始倒计时，倒计时{{
            form.yyCommonConfig.pickupStaffIncomeWithdrawalCycle
          }}s结束后没有扫码操作则自动变更状态，释放驿站订单</span
        >
        <div class="des-input">
          <el-input
            style="display: inline-block; width: 100px"
            v-model="form.yyCommonConfig.pickupStaffIncomeWithdrawalCycle"
            placeholder="7"
          ></el-input>
          <span>s</span>
          <el-button type="text" @click="updateOther">确定</el-button>
        </div>
      </div>
    </div>
    <el-dialog title="配送重量" v-model="dialogVisible" width="500px" @close="handleWeightClose">
      <el-form :model="weightForm" ref="refWeightForm" :rules="weightFormRules">
        <el-form-item label="最小重量" prop="minWeight">
          <el-input placeholder="5kg" v-model="weightForm.minWeight"></el-input>
        </el-form-item>
        <el-form-item label="最大重量" prop="maxWeight">
          <el-input placeholder="10kg" v-model="weightForm.maxWeight"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitWeight">{{ btnContent }}</el-button>
          <el-button type="primary" @click="weightREST(refWeightForm as FormInstance)">取消</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog title="配送时段" v-model="dialogVisible2" width="500px">
      <el-form :model="timeForm" ref="refTimeForm" :rules="timeFormRules">
        <!-- <el-form-item label="开始时间" prop="startTime">
          <el-time-picker
            placeholder="选择时间"
            type="datetime"
            format="YYYY-MM-DD HH:mm:ss"
            v-model="timeForm.startTime"
            style="width: 100%"
          ></el-time-picker>
        </el-form-item> -->
        <el-form-item label="时间范围" prop="timeRange">
          <!-- <el-time-picker
            placeholder="选择时间"
            type="datetime"
            format="YYYY-MM-DD HH:mm:ss"
            v-model="timeForm.endTime"
            style="width: 100%"
          ></el-time-picker> -->
          <el-date-picker
            v-model="dateTime"
            type="datetimerange"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            format="HH:mm:ss"
            time-format="hh:mm:ss"
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitTime">{{ btnContent }}</el-button>
          <el-button type="primary" @click="timeREST(refTimeForm as FormInstance)">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="tsx" setup name="otherConfig">
import dayjs from "dayjs";
import { Other } from "@/api/interface/province/config";
import {
  addOtherTimeAPI,
  addOtherWeightAPI,
  deleteOtherTimeAPI,
  deleteOtherWeightAPI,
  getOtherListAPI,
  updateOtherAPI,
  updateOtherTimeAPI,
  updateOtherWeightAPI
} from "@/api/modules/province/configAPI";
import { ElMessage, ElMessageBox, FormInstance, FormRules } from "element-plus";
import { onMounted, ref } from "vue";
import { Edit } from "@element-plus/icons-vue";
const radio1 = ref<string>("A");
const dialogVisible = ref<boolean>(false);
const dialogVisible2 = ref<boolean>(false);
const btnContent = ref<string>("添加");
let form = ref<Other.Data>({
  yyCommonConfig: {
    id: 0
  },
  deliveryTime: [],
  deliveryWeight: []
});
let dateTime = ref<string>("");
let weightForm = ref<Other.UpdateWeight>({});
let timeForm = ref<Other.UpdateTime>({});
const refWeightForm = ref<FormInstance>();
const refTimeForm = ref<FormInstance>();
const weightFormRules = ref<FormRules>({
  minWeight: [
    { required: true, message: "请输入最小重量", trigger: "blur" }
    // { type: "number", message: "请输入正确的最小重量", trigger: "blur" }
  ],
  maxWeight: [
    { required: true, message: "请输入最大重量", trigger: "blur" }
    // { type: "number", message: "请输入正确的最大重量", trigger: "blur" }
  ]
});
const timeFormRules = ref<FormRules>({});
const weightREST = (form: FormInstance) => {
  form.resetFields();
  dialogVisible.value = false;
};
const timeREST = (form: FormInstance) => {
  form.resetFields();
  dialogVisible2.value = false;
};
const addWeightIcon = () => {
  dialogVisible.value = true;
  btnContent.value = "添加";
  weightForm.value = {};
  // refWeightForm.resetFields();
};
const addTimeIcon = () => {
  dialogVisible2.value = true;
  btnContent.value = "添加";
  timeForm.value = {};
  // refTimeForm.value.resetFields();
};
const selectWeight = (item: Other.DeliveryWeight) => {
  console.log(item);
  weightForm.value.id = item.id;
  weightForm.value.maxWeight = Number(item.maxWeight);
  weightForm.value.minWeight = Number(item.minWeight);
  dialogVisible.value = true;
  btnContent.value = "修改";
  //   weightForm.value = item
};
const selectTime = (item: Other.DeliveryTime) => {
  console.log(item);
  timeForm.value.id = item.id;
  timeForm.value.startTime = item.startTime as string;
  timeForm.value.endTime = item.endTime as string;
  dialogVisible2.value = true;
  btnContent.value = "修改";
};
const btnRemove = (id: number) => {
  ElMessageBox.confirm(`此操作将永久删除该设施类型"${id}", 是否继续?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      deleteweight(id);
    })
    .catch(() => {
      ElMessage.error("取消删除!");
    });
};
const btnRemove2 = (id: number) => {
  ElMessageBox.confirm(`此操作将永久删除该设施类型"${id}", 是否继续?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      deletetime(id);
    })
    .catch(() => {
      ElMessage.error("取消删除!");
    });
};
const submitWeight = (): void => {
  let minWeight = Number(weightForm.value.minWeight);
  let maxWeight = Number(weightForm.value.maxWeight);
  if (minWeight > maxWeight) {
    ElMessage.error("最小重量不能大于最大重量!");
    return;
  }
  if (btnContent.value === "添加") {
    addweight();
  } else {
    updateWeight();
  }
};
const handleWeightClose = () => {
  dialogVisible.value = false;
  radio1.value = "";
};
const submitTime = (): void => {
  if (!dateTime.value) {
    ElMessage.error("请选择时间范围!");
    return;
  } else {
    console.log(dateTime.value);
    let startTime = dayjs(dateTime.value[0]).format("HH:mm:ss");
    let endTime = dayjs(dateTime.value[1]).format("HH:mm:ss");
    console.log(startTime, endTime);
    timeForm.value.startTime = startTime;
    timeForm.value.endTime = endTime;
    if (btnContent.value === "添加") {
      addTime();
    } else {
      updateTime();
    }
  }
};
const getOtherList = async () => {
  const { data, code } = await getOtherListAPI();
  if (code == "200" && data) {
    form.value = data;
  }
};
const updateOther = async () => {
  const { code, msg } = await updateOtherAPI(form.value.yyCommonConfig);
  if (code == "200") {
    ElMessage.success("修改成功!");
    getOtherList();
  } else {
    ElMessage.error("修改失败!" + msg);
  }
};
const addweight = async () => {
  const { code, msg } = await addOtherWeightAPI(weightForm.value);
  if (code == "200") {
    getOtherList();
    ElMessage.success("添加成功!");
    dialogVisible.value = false;
    radio1.value = "";
  } else {
    ElMessage.error("添加失败!" + msg);
  }
};
const addTime = async () => {
  const { code, msg } = await addOtherTimeAPI(timeForm.value);
  if (code == "200") {
    getOtherList();
    ElMessage.success("添加成功!");
    dialogVisible2.value = false;
    radio1.value = "";
  } else {
    ElMessage.error("添加失败!" + msg);
  }
};
const updateWeight = async () => {
  const { code, msg } = await updateOtherWeightAPI(weightForm.value);
  if (code == "200") {
    getOtherList();
    ElMessage.success("修改成功!");
    dialogVisible.value = false;
    radio1.value = "";
  } else {
    ElMessage.error("修改失败!" + msg);
  }
};
const updateTime = async () => {
  const { code, msg } = await updateOtherTimeAPI(timeForm.value);
  if (code == "200") {
    getOtherList();
    ElMessage.success("修改成功!");
    dialogVisible2.value = false;
    radio1.value = "";
  } else {
    ElMessage.error("修改失败!" + msg);
  }
};
const deletetime = async (id: number) => {
  const { code, msg } = await deleteOtherTimeAPI(Number(id));
  if (code == "200") {
    getOtherList();
    ElMessage.success("删除成功!");
  } else {
    ElMessage.error("删除失败!" + msg);
  }
};
const deleteweight = async (id: number) => {
  const { code, msg } = await deleteOtherWeightAPI(Number(id));
  if (code == "200") {
    getOtherList();
    ElMessage.success("删除成功!");
  } else {
    ElMessage.error("删除失败!" + msg);
  }
};
onMounted(() => {
  getOtherList();
});
</script>

<style lang="scss" scoped>
.app-container {
  padding: 10px;
  background-color: white;
}
.hr-line {
  padding-bottom: 10px;
  margin-bottom: 20px;
  border-bottom: 1px solid #e8e8e8;
  .other-title {
    margin-bottom: 12px;
    font-size: 14px;
    font-weight: bold;
  }
  .other-content {
    // position: relative;
    .des {
      font-size: 13px;
      color: gray;
      vertical-align: middle;
    }
    .des-input {
      display: inline-block;
      float: right;
      margin-top: -14px;
      margin-right: 50px;
      ::v-deep .el-input {
        display: inline;
      }
      span {
        display: inline-block;
        margin: 0 10px;
        font-size: 13px;
      }
      span:nth-last-child(2) {
        // color: red;
        width: 30px;
      }
    }
  }
}
.radio-btn {
  position: relative;
  display: inline-block;
  margin-right: 40px;
  background: whitesmoke;
  span {
    position: absolute;
    top: -8px;
    right: -8px;
    z-index: 1;
    display: inline-block;
    width: 16px;
    height: 16px;
    font-size: 12px;
    line-height: 14px;
    color: white;
    text-align: center;
    cursor: pointer;
    background-color: red;
    border-radius: 100%;
  }
  ::v-deep .el-radio-button__inner {
    // width: 120px;
    padding: 13px 30px;
    font-size: 12px;
    background: lightgray;
  }
}
.title {
  margin-bottom: 12px;
  font-size: 14px;
  font-weight: bold;
}
::v-deep .el-form-item__label {
  font-size: 13px;
  font-weight: 100;
  color: black;
}
::v-deep .el-button > span {
  font-size: 13px;
}
::v-deep .el-input__inner {
  font-size: 13px;
}
</style>
